package cn.edu.ctbu.service.impl;

import cn.edu.ctbu.DTO.EmpDTO;
import cn.edu.ctbu.DTO.EmpPageDTO;
import cn.edu.ctbu.VO.EmpLoginVO;
import cn.edu.ctbu.context.BaseContext;
import cn.edu.ctbu.entity.Employee;
import cn.edu.ctbu.exception.BaseException;
import cn.edu.ctbu.mapper.EmployeeMapper;
import cn.edu.ctbu.result.PageResult;
import cn.edu.ctbu.service.IEmployeeService;
import cn.edu.ctbu.utils.JWTUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-12
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Override
    public EmpLoginVO login(EmpDTO empDTO) {
        QueryWrapper queryWrapper=new QueryWrapper<>().eq("username",empDTO.getUsername());
        Employee emp = getOne(queryWrapper);
        if(emp==null){
            throw new BaseException("该用户不存在！");
        }
        //将密码改为mD5
        String pass=DigestUtils.md5DigestAsHex(empDTO.getPassword().getBytes());
        if(Objects.equals(emp.getPassword(), pass)){
            Map<String,Object>map=new HashMap<>();
            map.put("id",emp.getId());
            map.put("username",emp.getUsername());
            String token = JWTUtils.generateJwt(map);
            EmpLoginVO vo=new EmpLoginVO();
            vo.setUsername(emp.getUsername());
            vo.setPassword(pass);
            vo.setToken(token);
            BaseContext.setCurrentId(emp.getId());
            return vo;

        }else{
            throw new BaseException("密码错误！");
        }
    }

    @Override
    public PageResult getList(EmpPageDTO pageDTO) {
        PageResult pageResult=new PageResult();
        Page page=new Page<>().setCurrent(pageDTO.getPage()).setSize(pageDTO.getPageSize());
        QueryWrapper wrapper=new QueryWrapper<>();
        if(pageDTO.getName()!=null){
            wrapper.like("name",pageDTO.getName());
        }
        Page<Employee> pages=page(page,wrapper);
        pageResult.setRecords(pages.getRecords());
        pageResult.setTotal(pages.getTotal());
        return pageResult;
    }

    @Override
    public void Empstatus(Long id) {
        Employee byId = getById(id);
        Integer status = byId.getStatus();
        Employee emp=new Employee();
        emp.setId(id);
        if(status==1){
            emp.setStatus(0);
        }else{
            emp.setStatus(1);
        }
        Long currentId = BaseContext.getCurrentId();
        emp.setUpdateUser(currentId);
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
    }

    @Override
    public void addEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        save(employee);
    }

    @Override
    public void putEmp(Employee employee) {
        employee.setUpdateUser(BaseContext.getCurrentId());
        employee.setUpdateTime(LocalDateTime.now());
        updateById(employee);
    }
}
